From 0d9eac6e8ff6da0e3c8d49e8b29a9cad3b325851 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Sat, 19 Sep 2015 13:50:14 -0400 Subject: [PATCH] Extract contructor for Package where only root_package is needed There are many places where both source and its root_package() are used, but in these places, the only reason the source is created is to get to the root_package. This just extracts the source creation into a Package constructor for now, but I think this can be made to not use source at all. --- src/cargo/core/package.rs | 9 ++++++++- src/cargo/ops/cargo_clean.rs | 7 ++----- src/cargo/ops/cargo_doc.rs | 7 ++----- src/cargo/ops/cargo_pkgid.rs | 7 ++----- src/cargo/ops/cargo_run.rs | 6 ++---- src/cargo/ops/registry.rs | 14 ++++---------- 6 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/cargo/core/package.rs b/src/cargo/core/package.rs index f71f5025e..1860257ea 100644 --- a/src/cargo/core/package.rs +++ b/src/cargo/core/package.rs @@ -6,9 +6,10 @@ use semver::Version; use core::{Dependency, Manifest, PackageId, Registry, Target, Summary, Metadata}; use core::dependency::SerializedDependency; -use util::{CargoResult, graph}; +use util::{CargoResult, graph, Config}; use rustc_serialize::{Encoder,Encodable}; use core::source::Source; +use sources::PathSource; /// Informations about a package that is available somewhere in the file system. /// @@ -58,6 +59,12 @@ impl Package { } } + pub fn for_path(manifest_path: &Path, config: &Config) -> CargoResult { + let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(), + config)); + source.root_package() + } + pub fn dependencies(&self) -> &[Dependency] { self.manifest.dependencies() } pub fn manifest(&self) -> &Manifest { &self.manifest } pub fn manifest_path(&self) -> &Path { &self.manifest_path } diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index caae5e4f9..8a28168c9 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -3,9 +3,8 @@ use std::fs; use std::io::prelude::*; use std::path::Path; -use core::{PackageSet, Profiles, Profile}; +use core::{Package, PackageSet, Profiles, Profile}; use core::source::{Source, SourceMap}; -use sources::PathSource; use util::{CargoResult, human, ChainError, Config}; use ops::{self, Layout, Context, BuildConfig, Kind}; @@ -17,9 +16,7 @@ pub struct CleanOptions<'a> { /// Cleans the project from build artifacts. pub fn clean(manifest_path: &Path, opts: &CleanOptions) -> CargoResult<()> { - let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(), - opts.config)); - let root = try!(src.root_package()); + let root = try!(Package::for_path(manifest_path, opts.config)); let target_dir = opts.config.target_dir(&root); // If we have a spec, then we need to delete some package,s otherwise, just diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 763512caf..11e5d5bf7 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -3,9 +3,8 @@ use std::fs; use std::path::Path; use std::process::Command; -use core::PackageIdSpec; +use core::{Package, PackageIdSpec}; use ops; -use sources::PathSource; use util::{CargoResult, human}; pub struct DocOptions<'a> { @@ -15,9 +14,7 @@ pub struct DocOptions<'a> { pub fn doc(manifest_path: &Path, options: &DocOptions) -> CargoResult<()> { - let mut source = try!(PathSource::for_path(manifest_path.parent().unwrap(), - options.compile_opts.config)); - let package = try!(source.root_package()); + let package = try!(Package::for_path(manifest_path, options.compile_opts.config)); let mut lib_names = HashSet::new(); let mut bin_names = HashSet::new(); diff --git a/src/cargo/ops/cargo_pkgid.rs b/src/cargo/ops/cargo_pkgid.rs index 9e982aa37..ab5cfe691 100644 --- a/src/cargo/ops/cargo_pkgid.rs +++ b/src/cargo/ops/cargo_pkgid.rs @@ -1,16 +1,13 @@ use std::path::Path; use ops; -use core::{PackageIdSpec}; -use sources::{PathSource}; +use core::{PackageIdSpec, Package}; use util::{CargoResult, human, Config}; pub fn pkgid(manifest_path: &Path, spec: Option<&str>, config: &Config) -> CargoResult { - let mut source = try!(PathSource::for_path(&manifest_path.parent().unwrap(), - config)); - let package = try!(source.root_package()); + let package = try!(Package::for_path(manifest_path, config)); let lockfile = package.root().join("Cargo.lock"); let source_id = package.package_id().source_id(); diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 5db347623..da576189a 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -2,15 +2,13 @@ use std::path::Path; use ops::{self, ExecEngine, CompileFilter}; use util::{self, CargoResult, human, process, ProcessError}; -use sources::PathSource; +use core::Package; pub fn run(manifest_path: &Path, options: &ops::CompileOptions, args: &[String]) -> CargoResult> { let config = options.config; - let mut src = try!(PathSource::for_path(&manifest_path.parent().unwrap(), - config)); - let root = try!(src.root_package()); + let root = try!(Package::for_path(manifest_path, config)); let mut bins = root.manifest().targets().iter().filter(|a| { !a.is_lib() && !a.is_custom_build() && match options.filter { diff --git a/src/cargo/ops/registry.rs b/src/cargo/ops/registry.rs index a81dda28c..d526dc7fc 100644 --- a/src/cargo/ops/registry.rs +++ b/src/cargo/ops/registry.rs @@ -15,7 +15,7 @@ use core::{Package, SourceId}; use core::dependency::Kind; use core::manifest::ManifestMetadata; use ops; -use sources::{PathSource, RegistrySource}; +use sources::{RegistrySource}; use util::config; use util::{CargoResult, human, ChainError, ToUrl}; use util::config::{Config, ConfigValue, Location}; @@ -31,9 +31,7 @@ pub fn publish(manifest_path: &Path, token: Option, index: Option, verify: bool) -> CargoResult<()> { - let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(), - config)); - let pkg = try!(src.root_package()); + let pkg = try!(Package::for_path(&manifest_path, config)); let (mut registry, reg_id) = try!(registry(config, token, index)); try!(verify_dependencies(&pkg, ®_id)); @@ -263,9 +261,7 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> { Some(ref name) => name.clone(), None => { let manifest_path = try!(find_root_manifest_for_cwd(None)); - let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(), - config)); - let pkg = try!(src.root_package()); + let pkg = try!(Package::for_path(&manifest_path, config)); pkg.name().to_string() } }; @@ -325,9 +321,7 @@ pub fn yank(config: &Config, Some(name) => name, None => { let manifest_path = try!(find_root_manifest_for_cwd(None)); - let mut src = try!(PathSource::for_path(manifest_path.parent().unwrap(), - config)); - let pkg = try!(src.root_package()); + let pkg = try!(Package::for_path(&manifest_path, config)); pkg.name().to_string() } }; -- 2.30.2